package com.virtualightning.app

import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.MotionEvent
import android.view.View
import com.virtualightning.mvvm.base.BaseSpawnUI
import kotlinx.android.synthetic.main.activity_mdplayer.*
import tv.danmaku.ijk.media.player.IjkMediaPlayer


/**
 *  Anchor : Create by CimZzz
 *  Time : 2021/2/22 15:18:41
 *  Project : zxs-new
 *
 */
class MDPlayerActivity : BaseSpawnUI(), View.OnTouchListener {
//    private var mVRLibrary: MDVRLibrary? = null
    private var player: IjkMediaPlayer? = null


    override fun onRealCreated(savedInstanceState: Bundle?) {
        super.onRealCreated(savedInstanceState)
        player = IjkMediaPlayer()
        player?.setOnPreparedListener {
            it.start()
        }
//        player?.dataSource = "http://virtual-lightning.com/test.mp4"
        player?.dataSource = "http://t.gogoh5.com:10087/test.mp4"
//        player?.set
//        DataSource(RawDataSourceProvider(resources.openRawResourceFd(R.raw.test)))
        player?.isLooping = true
        player?.prepareAsync()
        setContentView(R.layout.activity_mdplayer)
        up.setOnTouchListener(this)
        down.setOnTouchListener(this)
        left.setOnTouchListener(this)
        right.setOnTouchListener(this)
        initVRLibrary()
    }


    private fun initVRLibrary() {
        // new instance
//        mVRLibrary = MDVRLibrary.with(this)
//            .displayMode(MDVRLibrary.DISPLAY_MODE_NORMAL)
//            .interactiveMode(MDVRLibrary.INTERACTIVE_MODE_TOUCH)
//            .asVideo { surface -> // IjkMediaPlayer or MediaPlayer
//                player?.setSurface(surface)
//            }
//            .build(surfaceView)
    }

    override fun onResume() {
        super.onResume()
//        mVRLibrary!!.onResume(this)
    }

    override fun onPause() {
        super.onPause()
//        mVRLibrary!!.onPause(this)
    }

    override fun onDestroy() {
        super.onDestroy()
//        mVRLibrary!!.onDestroy()
    }

    private val handler = Handler()
    private var isDown = false
    private var touchFlag = 0

    override fun onTouch(v: View?, event: MotionEvent): Boolean {
        when(event.action) {
            MotionEvent.ACTION_DOWN -> {
                when(v) {
                    up -> {
                        touchFlag = touchFlag or 0x1
                    }
                    down -> {
                        touchFlag = touchFlag or 0x2
                    }
                    left -> {
                        touchFlag = touchFlag or 0x4
                    }
                    right -> {
                        touchFlag = touchFlag or 0x8
                    }
                }
                if(!isDown) {
                    isDown = true
                    check()
                }
            }
            MotionEvent.ACTION_UP -> {
                when(v) {
                    up -> {
                        touchFlag = touchFlag and 0x1.inv()
                    }
                    down -> {
                        touchFlag = touchFlag and 0x2.inv()
                    }
                    left -> {
                        touchFlag = touchFlag and 0x4.inv()
                    }
                    right -> {
                        touchFlag = touchFlag and 0x8.inv()
                    }
                }
            }
        }
        return true
    }

    private fun check() {
        if(touchFlag == 0) {
            isDown = false
            return
        }
        var offsetX = 0f
        var offsetY = 0f
        if(touchFlag and 0x1 != 0) {
            offsetY -= 10f
        }
        if(touchFlag and 0x2 != 0) {
            offsetY += 10f
        }
        if(touchFlag and 0x4 != 0) {
            offsetX -= 10f
        }
        if(touchFlag and 0x8 != 0) {
            offsetX += 10f
        }
//        mVRLibrary?.handleDrag(offsetX, offsetY)
        handler.postDelayed({check()}, 20L)
    }
}